/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.newnode.view;

import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import offset.nodes.Constants;
import offset.nodes.client.chooser.view.TypeChooserDialog;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.model.Validator;

/**
 *
 * @author  Walter Lütgenau
 */
public class DataFolderPanel extends javax.swing.JPanel implements Validator {
    ServerModel serverModel = null;
    
    /** Creates new form FolderObjectPanel */
    public DataFolderPanel(ServerModel model) {
        initComponents();
        this.serverModel = model;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        schemaLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        schemaTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle"); // NOI18N
        nameLabel.setText(bundle.getString("fileObject.nameLabel")); // NOI18N

        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });

        schemaLabel.setText(bundle.getString("dataFolder.schema")); // NOI18N

        jButton1.setText(bundle.getString("fileObject.selectButton")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectSchema(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nameLabel)
                            .addComponent(schemaLabel))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(schemaTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)))))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(schemaLabel)
                    .addComponent(jButton1)
                    .addComponent(schemaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(184, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void selectSchema(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectSchema
        try {
            TypeChooserDialog dialog = new TypeChooserDialog(new JFrame(), true, serverModel, new QName(Constants.URI_NODES_DATA, ""));
            dialog.setVisible(true);
            if ( dialog.getReturnStatus() == TypeChooserDialog.RET_OK) {
                NodeTypeDefinition definition = dialog.getDefinition();
                schemaTextField.setText(definition.getName().toString());
                
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_selectSchema
    
    public String getPrimaryNodeType() {
        return schemaTextField.getText();
    }
    
    public boolean isValidPane() {
        boolean isValid = false;
        
        if ( nameTextField.getText() != null && nameTextField.getText().length() > 0 && schemaTextField.getText() != null && schemaTextField.getText().length() > 0)
            isValid = true;
        
        if ( !isValid ) {
            JOptionPane messageDialog = new JOptionPane();
            messageDialog.showMessageDialog(this, ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle").getString("datafolder.step1.invalid"));
        }
        
        return isValid;
    }
    
    public String getName() {
        return nameTextField.getText();
    }
    
    private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_nameTextFieldActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel schemaLabel;
    private javax.swing.JTextField schemaTextField;
    // End of variables declaration//GEN-END:variables
    
}
